package com.offcn.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.offcn.common.utils.JWTUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.offcn.member.entity.MemberEntity;
import com.offcn.member.service.MemberService;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员-会员表
 *
 * @author Since wu
 * @email 2676697841@qq.com
 * @date 2021-07-21 16:53:52
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据时间来统计注册会员数
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/countAccountCreate")
    public R countByDateTime(String beginTime,String endTime){
        List<Map<String, Object>> mapList = memberService.countByDateTime(beginTime, endTime);
        return R.ok().put("mapList",mapList);
    }

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @PostMapping("/login")
    public R login( String username, String password){
        return memberService.login(username, password);
    }

    /**
     * 刷新token
     * @param refreshToken
     * @return
     */
    @PostMapping("/refreshtoken")
    public R refreshToken(String refreshToken){
        //根据refreshToken从redis读取用户名
        String username = (String) stringRedisTemplate.boundHashOps(refreshToken).get("username");
        if(StringUtils.isEmpty(username)){
            return R.error("刷新token失败");
        }
        //根据用户名生成新的token
        String token = JWTUtil.generateToken(username);
        //更新token到redis
        stringRedisTemplate.boundHashOps(refreshToken).put("token",token);
        return R.ok().put("token",token).put("refreshToken",refreshToken);
    }

}
